package RentCarProject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

public class MyButton extends JButton {

    private int radius = 15; // 圆角半径

    public MyButton(String text) {
        super(text);
        setBackground(Color.cyan);
        setFont(new Font("宋体",Font.BOLD,15));
        setContentAreaFilled(false); // 设置按钮内容区域不透明
        setFocusPainted(false); // 设置按钮聚焦时不绘制边框
        setBorderPainted(false); // 设置按钮边框不可见

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // 在鼠标按下时更改背景色
                setBackground(Color.magenta);
                repaint();
//                setBorder(BorderFactory.createLineBorder(Color.white));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // 在鼠标释放时恢复原始背景色
                setBackground(Color.cyan);
//                setBorder(BorderFactory.createLineBorder(Color.black));
                repaint();

            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();
        int diameter = radius * 2;

        // 绘制圆角矩形
        g2.setColor(getBackground());
        g2.fillRoundRect(0, 0, width, height, diameter, diameter);

        // 绘制按钮文本
        FontMetrics fontMetrics = g2.getFontMetrics();
        int x = (width - fontMetrics.stringWidth(getText())) / 2;
        int y = (height - fontMetrics.getHeight()) / 2 + fontMetrics.getAscent();
        g2.setColor(getForeground());
        g2.drawString(getText(), x, y);

        g2.dispose();
    }

    @Override
    protected void paintBorder(Graphics g) {
        int width = getWidth();
        int height = getHeight();
        int diameter = radius * 2;

        // 绘制圆角矩形边框
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.LIGHT_GRAY);
        g2.drawRoundRect(0, 0, width - 1, height - 1, diameter, diameter);
        g2.dispose();
    }

    // 重写getPreferredSize方法以适应圆角大小
    @Override
    public Dimension getPreferredSize() {
        int width = Math.max(super.getPreferredSize().width, 2 * radius);
        int height = Math.max(super.getPreferredSize().height, 2 * radius);
        return new Dimension(width, height);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        MyButton myButton=new MyButton("jijni");
        myButton.setBounds(10, 10, 100, 50);
        frame.add(myButton);
    }
}